package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;


@Service("orderItemService")
@RabbitListener(queues = {"java-queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    /**
     *  测试监听队列
     *  注解所在的类需要注入容器
     *
     *      ack消息确认机制：
     *          1、自动确认模式
     *              消息一进入消费方法中，就默认被消费，如果消费方法执行一半，系统就宕机了，那未消费的消息就会全部丢失
     *          2、手动确认模式
     *              一个消息消费完，进行手动确认消费完，未确认的消息默认是Unacked状态，就算消费方法执行一半，系统就宕机了，
     *              但是Unacked状态的消息就会重新回到Ready状态
     *
     * @param message  接收到的消息，包含消息头、消息体等信息
     * @param orderEntity   接收到的对象的类型，用于自动封装消息体
     * @param channel   接收消息的管道
     */
    @RabbitHandler
    public void testListener(Message message, OrderEntity orderEntity, Channel channel){
        //System.out.println("收到消息："+message);
        System.out.println("收到对象："+orderEntity.getBillContent());
        MessageProperties messageProperties = message.getMessageProperties();
        long deliveryTag = messageProperties.getDeliveryTag();
        System.out.println("channel标签："+deliveryTag);
        try {
            if(deliveryTag%2==0){
                channel.basicAck(deliveryTag,false);
                System.out.println("已签收。。");
            }else {
                //两种方式都是拒签
                //long var1【管道标识，递增】, boolean var3【是否全部拒签】, boolean var4【是否将拒签的消息重新返回队列】
                //重新入队的消息会再次发送出去，直到消费完即止
                channel.basicNack(deliveryTag,false,true);
                //long var1【管道标识，递增】, boolean var3【是否将拒签的消息重新返回队列】
                //channel.basicReject(deliveryTag,false);
            }
        } catch (IOException e) {

        }

    }

    @RabbitHandler
    public void testListener(OrderItemEntity itemEntity){
        //System.out.println("收到消息："+message);
        System.out.println("收到对象："+itemEntity.getOrderSn());
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

}